using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using System.Collections;
using System.Drawing;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class Route : IRoute
    {
        /*定数*/
        public const string FEATURE_CD = Utility.FEATURE;
        public const string DAIBUN_CD = Utility.DAIBUN;
        public const string TSUUSHO_CD = Utility.TSUUSHO;

        private soDatasets objDSs = null;

        public Route(soDatasets objDSs)
        {
            this.objDSs = objDSs;
        }

        #region IRoute メンバ

        private string feature = String.Empty;
        public string Feature
        {
            get
            {
                return feature;
            }
            set
            {
                feature = value;
            }
        }

        private string daibun = String.Empty;
        public string Daibun
        {
            get
            {
                return daibun;
            }
            set
            {
                daibun = value;
            }
        }

        private string tsuusho = String.Empty;
        public string Tsuusho
        {
            get
            {
                return tsuusho;
            }
            set
            {
                tsuusho = value;
            }
        }

        public void MakeBufferForRoute(soDatasetVector objVector, soRecordset objRS)
        {
            soRecordset objSubRs = null;

            feature = objRS.GetFieldValue(FEATURE_CD).ToString();
            daibun = objRS.GetFieldValue(DAIBUN_CD).ToString();
            tsuusho = objRS.GetFieldValue(TSUUSHO_CD).ToString();

            objSubRs = objVector.Query(
                FEATURE_CD + " = '" + feature + "' and " +
                DAIBUN_CD + " = '" + daibun + "' and " +
                TSUUSHO_CD + " = '" + tsuusho + "'",
                true, null, "");

            if (objSubRs != null && objSubRs.RecordCount > 0)
            {
                if (!objSubRs.IsBOF())
                {
                    objSubRs.MoveFirst();
                }

                soGeoLine objFullRoute = null;
                soGeoLine objLine = null;
                for (; !objSubRs.IsEOF(); objSubRs.MoveNext())
                {
                    objLine = objSubRs.GetGeometry() as soGeoLine;
                    if (objFullRoute == null)
                    {
                        objFullRoute = objLine;
                        continue;
                    }
                    if (!objFullRoute.Joint(objLine))
                    {
                        Marshal.ReleaseComObject(objLine);
                        objLine = null;
                        objSubRs.Close();
                        Marshal.ReleaseComObject(objSubRs);
                        objSubRs = null;
                        Marshal.ReleaseComObject(objFullRoute);
                        objFullRoute = null;

                        throw new Exception("Joint line failed!");
                    }
                }

                soGeoRegion objBuffer = objFullRoute.SpatialOperator.Buffer2(
                            Utility.BUFFER_ALARM_DISTANCE_2, Utility.BUFFER_ALARM_DISTANCE_2, 0);
                Utility.routeLnWithBfr[objFullRoute] = objBuffer;

                Marshal.ReleaseComObject(objLine);
                objLine = null;
                objSubRs.Close();
                Marshal.ReleaseComObject(objSubRs);
                objSubRs = null;
                Marshal.ReleaseComObject(objFullRoute);
                objFullRoute = null;
                //Marshal.ReleaseComObject(objBuffer);
                //objBuffer = null;
            }
        }

        #endregion
    }
}
